package com.ujcms.cms.core.domain;

import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonIncludeProperties;
import com.ujcms.cms.core.domain.base.AttachmentBase;
import com.ujcms.commons.file.FilesEx;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotNull;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.hibernate.validator.constraints.Length;

import java.io.Serial;
import java.io.Serializable;
import java.time.OffsetDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 附件实体类
 *
 * @author PONY
 */
@EqualsAndHashCode(callSuper = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties("handler")
@Data
@TableName("attachment")
public class Attachment extends AttachmentBase implements Serializable {
    @Serial
    private static final long serialVersionUID = 1L;

    public Attachment() {
    }

    /**
     * 数据库表名
     */
    public static final String TABLE_NAME = "attachment";

    /**
     * 附件ID
     */
    @NotNull
    @Schema(description = "附件ID")
    private Long id = 0L;

    /**
     * 站点ID
     */
    @NotNull
    @Schema(description = "站点ID")
    private Long siteId = 0L;

    /**
     * 用户ID
     */
    @NotNull
    @Schema(description = "用户ID")
    private Long userId = 0L;

    /**
     * 文件名称
     */
    @Length(max = 150)
    @NotNull
    @Schema(description = "文件名称")
    private String name = "";

    /**
     * 存储路径
     */
    @Length(max = 160)
    @NotNull
    @Schema(description = "存储路径")
    private String path = "";

    /**
     * 访问路径
     */
    @Length(max = 160)
    @NotNull
    @Schema(description = "访问路径")
    private String url = "";

    /**
     * 文件长度
     */
    @NotNull
    @Schema(description = "文件长度")
    private Long length = 0L;

    /**
     * 创建时间
     */
    @NotNull
    @Schema(description = "创建时间")
    private OffsetDateTime created = OffsetDateTime.now();

    /**
     * 是否使用
     */
    @NotNull
    @Schema(description = "是否使用")
    private Boolean used = false;


    public Attachment(Long siteId, Long userId, String name, String path, String url, Long length) {
        this.siteId = siteId;
        this.userId = userId;
        this.name = name;
        this.path = path;
        this.url = url;
        this.length = length;
    }

    public String getSize() {
        return FilesEx.getSize(this.length);
    }

    @JsonIncludeProperties({"id", "username", "nickname"})
    private User user = new User();
    @JsonIncludeProperties({"id", "name"})
    private Site site = new Site();
    private List<AttachmentRefer> referList = new ArrayList<>();
}